#!/bin/bash
watcher=/usr/local/emhttp/plugins/dynamix.file.integrity/scripts/watcher
conf=/etc/inotifywait.conf

watcher.start() {
  if [[ -z $(pgrep -f $watcher) ]]; then
    source $conf 2>/dev/null
    $watcher "$cmd" "$method" "$exclude" "$disks"
    echo "watcher started"
  else
    echo "watcher already running!"
  fi
}

watcher.stop() {
  if [[ -n $(pgrep -f $watcher) ]]; then
    kill $(ps -ef|grep 'inotifywait.*disk[0-9]*$'|awk '{print $2}')
    pkill -f $watcher
    for timer in {1..5}; do
      [[ -z $(pgrep -f $watcher) ]] && break
      sleep 1
    done
    [[ $timer -lt 5 ]] && echo "watcher stopped" || echo "watcher did not stop"
  else
    echo "watcher not running!"
  fi
  rm -f /var/run/hash.pipe
}

watcher.restart() {
  watcher.stop
  sleep 1
  watcher.start
}

case $1 in
 start) watcher.start ;;
 stop) watcher.stop ;;
 restart) watcher.restart ;;
 *) echo "Usage: $(basename $0) start|stop|restart"
esac
